package com.ccl.iot.views;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 创建者     王浩
 * 创建时间   2016/12/5 9:40
 * 描述	     可上下滑动的列表选择器选择, 只显示三条item, 这里主要是用在智能控制中选择时间段用的自定义控件,
 *           类似三级联动里的单级界面, 只能显示数字, 范围在布局文件中定义.
 */
public class TimePickerView extends ListView {

    private int mRange;     //显示数字的范围
    private int mWidth;
    private int mHeight;
    private MyAdapter mMyAdapter;

    private boolean mInit = false;      //初始化滚动

    private OnPickerListener mListener;
    private int mInitPosition;          //初始化滚动的Position

    /**
     * 选中的监听器
     */
    public interface OnPickerListener{
        public void onPicker(int picker, int id);
    }

    public void setOnPickerListener(OnPickerListener l){
        mListener = l;
    }

    public TimePickerView(Context context) {
        this(context, null);
    }

    public TimePickerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TimePickerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取范围
        mRange = attrs.getAttributeIntValue(null, "Range", 0);

        //去掉点击时的黄色背景
        this.setCacheColorHint(0);
        this.setSelector(getResources().getDrawable(android.R.color.transparent));

        mInit = true;

        this.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                switch (scrollState) {
                    case SCROLL_STATE_IDLE:     //当滚动停止

                        //获取第一个可见的view的Position
                        int p = view.getFirstVisiblePosition();
                        //获取第一个可见的view
                        View c = getChildAt(0);
                        TextView tv, top, bottom;   //上中下三个item
                        int picker = 0;

                        //判断向上显示还是向下显示
                        if (Math.abs(c.getTop()) <= c.getHeight() / 2) {
                            setSelection(p);
                            top = (TextView) getChildAt(0);
                            tv = (TextView) getChildAt(1);
                            bottom = (TextView) getChildAt(2);
                        } else {
                            setSelection(p + 1);
                            top = (TextView) getChildAt(1);
                            tv = (TextView) getChildAt(2);
                            bottom = (TextView) getChildAt(3);
                        }

                        //突出第二个item, 也就是选中的item
                        if (tv != null) {
                            tv.setTextSize(40);
                            tv.setTextColor(Color.parseColor("#ff8800"));
                            picker = Integer.parseInt(tv.getText().toString().trim());
                        }

                        //恢复其他两个可见的item
                        if (top != null) {
                            top.setTextSize(20);
                            top.setTextColor(Color.parseColor("#aaaaaa"));
                        }

                        if (bottom != null) {
                            bottom.setTextSize(20);
                            bottom.setTextColor(Color.parseColor("#aaaaaa"));
                        }

                        //选中的回调, 传入选中的数字
                        if(mListener != null){
                            mListener.onPicker(picker, getId());
                        }

                        break;
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                //初始化界面
                if (mInit) {
                    mInit = false;
                    TextView tv = (TextView) getChildAt(1);
                    if (tv != null) {
                        tv.setTextSize(40);
                        tv.setTextColor(0xffffff);
                    }
                }/*else{
                    TextView tv, top, bottom;   //上中下三个item
                }*/

            }
        });

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = getWidth();
        mHeight = getHeight();
        mMyAdapter = new MyAdapter();
        setAdapter(mMyAdapter);
        mInit = true;
        setSelection((Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2 % mRange - 1)+mInitPosition);
    }

    /**
     * 设置初始化的数字
     * @param init
     */
    public void setInit(int init){
        mInitPosition = init;
        mInit = true;
        setSelection((Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2 % mRange - 1)+init);
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                TextView tv = new TextView(getContext());
                tv.setWidth(mWidth);
                tv.setHeight(mHeight / 3);
                tv.setGravity(Gravity.CENTER);
                convertView = tv;
            }

            TextView tv = ((TextView) convertView);
            String s = String.format("%02d", position % mRange);
            tv.setText(s);
            tv.setTextSize(20);
            tv.setTextColor(Color.parseColor("#aaaaaa"));
            return convertView;
        }
    }

}
